﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyQQListView;
using System.Drawing.Imaging;
using System.Drawing.Text;
using Lyh.MyQQListView.Properties;
using MyQQListView.Enum;
using Lyh.MyQQListView.Enum;

namespace MyQQListView
{
    public partial class FriendControl : UserControl
    {
        #region --私有变量--

        private Graphics g = null;
        private Bitmap bgImg = null;
        private Bitmap headImg = null;
        private Bitmap headBorder = null;

        ToolTip tip = new ToolTip();

        private int x = 8;
        private int y = 8;
        private int offset = 4;

        private Font f = null;

        private Friend friend = null;
        private bool isSelected = false;

        private HeadImageMode headImageMode = HeadImageMode.Small;


        #endregion

        #region --委托变量--

        public delegate void SelectedEventHandler(Friend sender);
        public event SelectedEventHandler Selecting;

        public delegate void OpenChatEventHandler(Friend sender);
        public event OpenChatEventHandler OpenChat;

        public delegate void ShowContextMenuEventHandler(FriendControl sender, MouseEventArgs e);
        public event ShowContextMenuEventHandler ShowContextMenu;

        #endregion

        #region --公共变量--

        [Category("数据")]
        [Browsable(false)]
        [Description("是否选中")]
        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                isSelected = value;
                if (value)
                {
                    bgImg = Resources.friendTitleOpenbg;
                    headBorder = Resources.Padding4Press;
                }
                else
                {
                    bgImg = null;
                    headBorder = Resources.Padding4Normal;
                }
                this.Invalidate();
            }
        }

        [Category("数据")]
        [Browsable(false)]
        [Description("是否选中")]
        public Friend Friend
        {
            get
            {
                return friend;
            }
            set
            {
                friend = value;
                ChangeInfo(friend);
                this.Invalidate();
            }
        }

        [Category("数据")]
        [Description("头像大小")]
        [Browsable(true)]
        public HeadImageMode HeadImageMode
        {
            get { return headImageMode; }
            set { headImageMode = value; }
        }

        #endregion

        public FriendControl()
        {
            bgImg = null;
            this.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
            this.BackColor = Color.Transparent;
            try
            {
                f = new Font("微软雅黑", 9F, FontStyle.Regular);
            }
            catch (Exception)
            {
                f = new Font("宋体", 9F, FontStyle.Regular);
            }
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int headImageHeight = ((int)HeadImageMode) + 2 * offset;
            g = e.Graphics;
            if (bgImg != null)
            {
                g.DrawImage(bgImg, new Rectangle(0, 0, this.Width, 1), 0, 0, bgImg.Width, 1, GraphicsUnit.Pixel);
                g.DrawImage(bgImg, new Rectangle(0, 1, 1, this.Height - 2), 0, 1, 1, bgImg.Height - 2, GraphicsUnit.Pixel);
                g.DrawImage(bgImg, new Rectangle(1, 1, this.Width - 2, this.Height - 2), 1, 1, bgImg.Width - 2, bgImg.Height - 2, GraphicsUnit.Pixel);
                g.DrawImage(bgImg, new Rectangle(this.Width - 1, 1, 1, this.Height - 2), bgImg.Width - 1, 1, 1, bgImg.Height - 2, GraphicsUnit.Pixel);
                g.DrawImage(bgImg, new Rectangle(0, this.Height - 1, this.Width, 1), 0, bgImg.Height - 1, bgImg.Width, 1, GraphicsUnit.Pixel);
            }
            if (!Friend.IsSysHead)
                headBorder = Resources.Padding4Normal;
            if (headBorder != null)
            {
                headBorder = new Bitmap(headBorder, headImageHeight, headImageHeight);
                g.DrawImage(headBorder, new Rectangle(offset, offset, headImageHeight, headImageHeight), 0, 0, headImageHeight, headImageHeight, GraphicsUnit.Pixel);
            }
            g.DrawImage(headImg, new Rectangle(x, y, (int)HeadImageMode, (int)HeadImageMode), 0, 0, (int)HeadImageMode, (int)HeadImageMode, GraphicsUnit.Pixel);

            switch (HeadImageMode)
            {
                case HeadImageMode.Small:
                    SizeF sf = g.MeasureString(Friend.NikeName, f);
                    g.DrawString(Friend.NikeName, f, new SolidBrush(Color.FromArgb(240, 0, 0, 0)), (int)HeadImageMode + 20, (((int)HeadImageMode) + 4 * offset - sf.Height) / 2);
                    g.DrawString(Friend.Description, f, new SolidBrush(Color.FromArgb(100, 0, 0, 0)), (int)HeadImageMode + 20 + sf.Width, (((int)HeadImageMode) + 4 * offset - sf.Height) / 2);
                    break;
                case HeadImageMode.Normal:
                    SizeF sf2 = g.MeasureString(Friend.NikeName, f);
                    g.DrawString(Friend.NikeName, f, new SolidBrush(Color.FromArgb(240, 0, 0, 0)), (int)HeadImageMode + 20, offset);
                    SizeF sf3 = g.MeasureString(Friend.Description, f);
                    g.DrawString(Friend.Description, f, new SolidBrush(Color.FromArgb(100, 0, 0, 0)), (int)HeadImageMode + 20, offset + sf2.Height);
                    g.DrawImage(Resources.mail, new Rectangle((int)HeadImageMode + 20, (int)(sf2.Height + sf3.Height), 18, 18), 0, 0, 18, 18, GraphicsUnit.Pixel);
                    break;
                case HeadImageMode.Large:
                    SizeF sf4 = g.MeasureString(Friend.NikeName, f);
                    g.DrawString(Friend.NikeName, f, new SolidBrush(Color.FromArgb(240, 0, 0, 0)), (int)HeadImageMode + 20, offset*3);
                    SizeF sf5 = g.MeasureString(Friend.Description, f);
                    g.DrawString(Friend.Description, f, new SolidBrush(Color.FromArgb(100, 0, 0, 0)), (int)HeadImageMode + 20, offset*4 + sf4.Height);
                    g.DrawImage(Resources.mail, new Rectangle((int)HeadImageMode + 20, (int)(sf4.Height + sf5.Height + offset * 4), 18, 18), 0, 0, 18, 18, GraphicsUnit.Pixel);
                    break;
                default:
                    break;
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (ShowContextMenu != null)
                ShowContextMenu(this, e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            if (!IsSelected)
            {
                bgImg = Resources.friendTitlebg;
                headBorder = Resources.Padding4Select;
                this.Invalidate();
            }

        }

        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            tip.ToolTipTitle = friend.NikeName;
            tip.UseAnimation = true;
            tip.UseFading = true;
            tip.SetToolTip(this, Friend.Description);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (!IsSelected)
            {
                bgImg = null;
                headBorder = null;
                this.Invalidate();
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (!IsSelected)
            {
                IsSelected = true;
                bgImg = Resources.friendTitleOpenbg;
                headBorder = Resources.Padding4Press;
                this.Invalidate();
                if (Selecting != null)
                    Selecting(this.Friend);
            }
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            if (e.Button == MouseButtons.Left)
            {
                if (!IsSelected)
                {
                    bgImg = Resources.friendTitleOpenbg;
                    headBorder = Resources.Padding4Press;
                    this.Invalidate();
                }
                if (OpenChat != null)
                    OpenChat(this.Friend);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            int Rgn = Win32.CreateRoundRectRgn(0, 0, this.Width + 1, this.Height + 1, 5, 5);
            Win32.SetWindowRgn(this.Handle, Rgn, true);
            this.Invalidate();
        }

        public void SelectCurrent(bool flag)
        {
            if (flag)
            {
                OnMouseClick(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
            }
            else
            {
                OnMouseLeave(null);
            }
        }

        public void Call(int Jsec)
        {
            if (Jsec % 2 == 0)
            {
                x = 8;
                y = 6;
            }
            else
            {
                x = 7;
                y = 5;
            }
            //this.Invalidate(new Rectangle(7, 5, 41, 41));
            this.Invalidate(new Rectangle(7, 5, (int)HeadImageMode + 1, (int)HeadImageMode + 1));
        }

        //public void UpdateState(UserInfo Info)
        //{
        //    //ChangeInfo(Info);
        //    this.Invalidate(new Rectangle(7, 5, 41, 41));
        //}

        private void ChangeInfo(Friend friend)
        {
            headImg = ResClass.GetHead(friend.HeadImage);
            if (headImg == null) headImg = Resources.Default;
            headImg = new Bitmap(headImg, (int)HeadImageMode, (int)HeadImageMode);
            switch (friend.UserState)
            {
                case UserState.Online:
                    break;
                case UserState.QMe:
                    break;
                case UserState.Away:
                    break;
                case UserState.Busy:
                    break;
                case UserState.Mute:
                    break;
                case UserState.Invisible:
                case UserState.Offline:
                    SetImageToGray(headImg);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 把图片从彩色转换到灰色
        /// </summary>
        /// <param name="img">图片</param>
        private void SetImageToGray(Bitmap img)
        {
            for (int x = 0; x < img.Width; x++)
            {
                for (int y = 0; y < img.Height; y++)
                {

                    Color pixel = img.GetPixel(x, y);
                    if (pixel.A == 255 && (pixel.Name != "ffffffff"))
                    {
                        int val = (pixel.R + pixel.G + pixel.B) / 3;
                        img.SetPixel(x, y, Color.FromArgb(val, val, val));
                    }
                }
            }
        }

        internal void Selected()
        {
            OnMouseClick(new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // FriendControl
            // 
            this.Name = "FriendControl";
            this.ResumeLayout(false);

        }
    }
}